import React, { useEffect } from "react";
import { Modal, Form, Input, Select, Button } from "antd";

interface ReturnApplicationValues {
  product_id: number;
  applicant_id: number;
  quantity: number;
  return_status: number;
}

interface ReturnApplicationModalProps {
  title: string;
  visible: boolean;
  onCancel: () => void;
  onOk: (values: ReturnApplicationValues) => void;
  defaultValue: ReturnApplicationValues;
  productList?: any[];
  users: any[];
}

const { Option } = Select;

const ReturnApplicationModal: React.FC<ReturnApplicationModalProps> = ({
  title,
  visible,
  onCancel,
  onOk,
  defaultValue,
  productList = [],
  users = [],
}) => {
  const [form] = Form.useForm<ReturnApplicationValues>();

  const handleOk = async () => {
    try {
      const values = await form.validateFields();
      onOk(values);
      form.resetFields();
    } catch (errorInfo) {
      console.log("Failed:", errorInfo);
    }
  };

  useEffect(() => {
    if (Object.keys(defaultValue).length === 0) {
      form.resetFields();
    } else {
      form.setFieldsValue({
        ...defaultValue,
      });
    }
  }, [defaultValue, visible]);

  return (
    <Modal
      title={title}
      visible={visible}
      onOk={handleOk}
      onCancel={onCancel}
      footer={[
        <Button key="cancel" onClick={onCancel}>
          取消
        </Button>,
        <Button key="ok" type="primary" onClick={handleOk}>
          确定
        </Button>,
      ]}
    >
      <Form form={form} initialValues={{ ...defaultValue }}>
        <Form.Item
          label="选择产品"
          name="product_id"
          rules={[
            {
              required: !Object.keys(defaultValue).length,
              message: "请选择产品",
            },
          ]}
          style={{
            display: Object.keys(defaultValue).length ? "none" : "block",
          }}
        >
          <Select placeholder="请选择产品">
            {productList.map((product) => (
              <Option key={product.id} value={product.id}>
                {product.product_name}
              </Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label="归还数量"
          name="quantity"
          rules={[
            {
              required: !Object.keys(defaultValue).length,
              message: "请输入有效的归还数量",
            },
          ]}
          style={{
            display: Object.keys(defaultValue).length ? "none" : "block",
          }}
        >
          <Input type="number" placeholder="请输入归还数量" />
        </Form.Item>

        <Form.Item
          label=" 归还人id"
          name="applicant_id"
          rules={[
            {
              required: true,
              message: "请选择供应商",
            },
          ]}
        >
          {/* Add a Select component for selecting suppliers */}
          <Select placeholder="请选择供应商">
            {/* Map through the 'users' array to display supplier options */}
            {users.map((user) => (
              <Option key={user.user_id} value={user.user_id}>
                {user.username}
              </Option>
            ))}
          </Select>
        </Form.Item>

        <Form.Item
          label="归还状态"
          name="return_status"
          rules={[
            {
              required: true,
              message: "请选择归还状态",
            },
          ]}
        >
          <Select placeholder="请选择归还状态">
            <Option value={1}>待归还</Option>
            <Option value={2}>审核中</Option>
            <Option value={3}>已归还</Option>
          </Select>
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default ReturnApplicationModal;
